package me.like.diandian.fragment;


import androidx.core.app.ActivityCompat;

import com.jakewharton.rxbinding2.view.RxView;

import io.reactivex.functions.Consumer;
import me.like.diandian.BuildConfig;
import me.like.diandian.R;
import me.like.diandian.activity.AboutActivity;
import me.like.diandian.activity.ClearActivity;
import me.like.diandian.activity.LoginActivity;
import me.like.diandian.base.BaseFragment;
import me.like.diandian.databinding.FragmentMeBinding;
import me.like.diandian.util.CommonUtils;
import me.like.diandian.util.SPUtils;
import me.like.diandian.util.updata.UpdataDialog;
import me.like.diandian.view.ToastView;

/**
 * Created by shousakai on 2019/6/21.
 */

public class MeFragment extends BaseFragment<FragmentMeBinding> {
    @Override
    protected void initView() {
        binding.tlvTitle.setTitle(getString(R.string.tab3_title));
        binding.tlvTitle.setBackgroundColor(ActivityCompat.getColor(activity, R.color.white));


        RxView.longClicks(binding.ivAvatar).subscribe(v -> {
            if (BuildConfig.DEBUG) {
                SPUtils.setOnLine(activity, !SPUtils.isOnLine(activity));
                if (SPUtils.isOnLine(activity)) {
                    ToastView.initToast().textToast(activity, "切换到生产环境，请杀死进程");
                } else {
                    ToastView.initToast().textToast(activity, "切换到测试环境，请杀死进程");
                }
            }
        });
        onClick(binding.llAbout, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                AboutActivity.launch(activity);
            }
        });
        onClick(binding.llClear, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                ClearActivity.launch(getActivity());
            }
        });

        onClick(binding.llQuit, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                new UpdataDialog(activity, "确定要退出么？", "退出登陆", false) {
                    @Override
                    public void onOkClick() {
                        super.onOkClick();
                        SPUtils.clearByKey2(SPUtils.TOKEN, activity);
                        LoginActivity.launch(activity, false);
                        activity.finish();
                    }
                }.show();
            }
        });
        onClick(binding.llShare, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                CommonUtils.share(activity, getString(R.string.app_name));
            }
        });

        String phone = SPUtils.getUserLogin(activity).getPhone();
        binding.tvUserName.setText(CommonUtils.changeTel(phone));

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_me;
    }
}
